#!/usr/bin/python3

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, GLib, GdkPixbuf

import subprocess
from subprocess import Popen, PIPE

import os
import sys
import glob

SELFDIR=os.path.dirname(os.path.realpath(__file__))+"/"

def cmdexec(cmd1="",cmd2=""):
  global win
  cmd=cmd1
  if cmd2!="": cmd=cmd2
  cmd=cmd.strip()
  if cmd.endswith('&'):
    print(cmd)
    cmd=cmd[:-1]
    print(cmd)
    subprocess.Popen(cmd,shell=True)
    return
  else:
    a=subprocess.getoutput(cmd)
    return a

class HelpWindow(Gtk.Window):    
  def singlebutton(self,text,bild,kommando,kommando2):
    btn0 = Gtk.Button(xalign=0)
    btn0.connect("clicked", kommando, kommando2)
    btn0_grid=Gtk.Grid(margin=10,column_spacing=10,row_spacing=10,hexpand=True,column_homogeneous=True,row_homogeneous=False)
    btn0_label=Gtk.Label(label=text,xalign=0.5);
    btn0_img=Gtk.Image()
    btn0_img.set_from_pixbuf(GdkPixbuf.Pixbuf.new_from_file_at_scale(filename=SELFDIR+bild,width=-1, height=60, preserve_aspect_ratio=True))
    btn0_grid.attach(btn0_img, 0, 0, 1, 1)
    btn0_grid.attach(btn0_label, 0, 1, 1, 1)
    btn0.add(btn0_grid)
    return btn0
    
  def __init__(self):
    global SELFDIR,ONLINE
    super().__init__(title="XLinux Hilfecenter")
    if os.path.isfile(SELFDIR+"help.png"):
      self.set_icon_from_file(SELFDIR+"help.png")
    self.set_position(Gtk.WindowPosition.CENTER)
    grid = Gtk.Grid(margin=10,column_spacing=10,row_spacing=10,hexpand=True,column_homogeneous=True,row_homogeneous=False)
    #grid.override_background_color(Gtk.StateType.NORMAL, Gdk.RGBA(.5,.5,.5,.5))
    symbolsize=60

    if not ONLINE:
      label=Gtk.Label(halign=3,justify=2);
      label.set_markup("<b>Keine Verbindung zum Internet!</b>\nDaher sind einige Funktionen nicht verfügbar.")
      grid.attach(label,0,0,3,1)

    
    # Kurzeinweisung
    btn0=self.singlebutton("Kurz-Einführung", "kurzeinweisung.svg", self.wincmd, "/opt/XLinux/Hilfe/Einweisung/bildergalerie.py &")
    grid.attach(btn0, 0, 1, 1, 1)
    if not os.path.isfile("/opt/XLinux/Hilfe/Einweisung/bildergalerie.py"):
      btn0.set_sensitive(False)

    #WLAN-Verbinden
    btn1=self.singlebutton("WLAN verbinden", "wifi.svg", self.wincmd, "exo-open '/opt/XLinux/Hilfe/WLAN verbinden.pdf' &")
    grid.attach(btn1, 1, 1, 1, 1)

    # Handbuch
    btn4=self.singlebutton("Handbuch (PDF)", "handbuch.svg", self.wincmd, "exo-open /opt/XLinux/Hilfe/Handbuch_*.pdf &")
    grid.attach(btn4, 2, 1, 1, 1)
    if len(glob.glob("/opt/XLinux/Hilfe/Handbuch_*.pdf"))==0:
      btn4.set_sensitive(False)

    # Hilfe & Anleitungen (Online)
    btn2=self.singlebutton("Hilfe & Anleitungen", "onlinehilfe.svg", self.wincmd, "exo-open https://support.mastro-it.de/service?uuid="+cmdexec("xlinux-systemid")+" &")
    grid.attach(btn2, 0, 2, 1, 1)
    if not ONLINE:
      btn2.set_sensitive(False)

    # Fernwartung   
    btn3=self.singlebutton("kostenpflichtige Hilfe", "fernwartung.svg", self.open_remote_service, "" )
    grid.attach(btn3, 1, 2, 1, 1)
    if not ONLINE:
      btn3.set_sensitive(False)
      
    # Softwarecenter
    btn5=self.singlebutton("Anwendungen & Spiele", "softwarecenter.svg", self.wincmd, "/opt/XLinux/softwarecenter/Softwarecenter.py &" )
    grid.attach(btn5, 2, 2, 1, 1)
    if not ONLINE:
      btn5.set_sensitive(False)
      
    self.show_never_again=Gtk.CheckButton(label=" Hilfecenter nach der Anmeldung anzeigen.",xalign=0,hexpand=False)
    self.show_never_again.connect("clicked",self.on_autostart_click)
    if os.path.isfile(os.environ['HOME']+"/.config/xlinux_start_help"):
      self.show_never_again.set_active(True)
    else:
      self.show_never_again.set_active(False)
    grid.attach(self.show_never_again, 0, 3, 3, 1)

    self.add(grid);
    
  def on_autostart_click(self,button):
    if self.show_never_again.get_active()==True:
      cmdexec("touch ~/.config/xlinux_start_help")
    else:
      cmdexec("rm ~/.config/xlinux_start_help")

  def wincmd(self,button,befehl):
    cmdexec(befehl)
    Gtk.main_quit()

  def open_remote_service(self,button,*_):
    self.hide()
    win=RemoteSupportWindow()
    win.connect("destroy", win.open_main)
    win.show_all()


class RemoteSupportWindow(Gtk.Window):    
  def __init__(self):
    global SELFDIR,ONLINE
    super().__init__(title="XLinux Hilfecenter")
    if os.path.isfile(SELFDIR+"help.png"):
      self.set_icon_from_file(SELFDIR+"help.png")
    self.set_position(Gtk.WindowPosition.CENTER)
    grid = Gtk.Grid(margin=10,column_spacing=20,row_spacing=20,hexpand=True,column_homogeneous=True,row_homogeneous=False)

    label=Gtk.Label(xalign=0)
    label.set_markup("<b>Telefonische Hilfe / Fernwartung</b>\nWenn Sie individuelle Fragen haben, können wir Ihnen persönlich helfen.\nDiese Dienstleistung wird Ihnen nach Zeitaufwand in Rechnung gestellt.\nMehr Informationen über unsere Webseite: <a href='https://www.mastro-it.de'>www.mastro-it.de</a>\n\n<b>Die nächsten Schritte:</b>")
    grid.attach(label, 0, 0, 3, 1)
    btn3_img=Gtk.Image()
    btn3_img.set_from_pixbuf(GdkPixbuf.Pixbuf.new_from_file_at_scale(filename=SELFDIR+"fernwartung.svg",width=100, height=100, preserve_aspect_ratio=True))
    grid.attach(btn3_img, 3, 0, 1, 1)
    
    label_schritt1=Gtk.Label(xalign=0)
    label_schritt1.set_markup("<b>1.</b> Termin für telefonische Unterstützung buchen: <a href='https://www.mastro-it.de/rueckruf'>Termin buchen</a>")
    grid.attach(label_schritt1, 0, 1, 4, 1)

    label_schritt2=Gtk.Label(xalign=0)
    label_schritt2.set_markup("<b>2.</b> Zum Termin müssen Sie telefonisch erreichbar sein und der Computer muss mit dem Internet verbunden sein.")
    grid.attach(label_schritt2, 0, 2, 4, 1)

    label_schritt3=Gtk.Label(xalign=0)
    label_schritt3.set_markup("<b>3.</b> Wir rufen Sie an und werden Sie bei Bedarf auffordern, den 'Fernwartung starten'-Button zu drücken.")
    grid.attach(label_schritt3, 0, 3, 4, 1)

    btn3 = Gtk.Button(label='Fernwartung starten',xalign=0.5)
    btn3.connect("clicked", self.wincmd, "/opt/XLinux-Fernwartung/2022_fernwartung.sh &")
    grid.attach(btn3, 0, 4, 1, 1)

    btn3 = Gtk.Button(label='Zurück',xalign=0.5)
    btn3.connect("clicked", self.open_main)
    grid.attach(btn3, 3, 4, 1, 1)
    
    self.add(grid);

  def wincmd(self,button,befehl):
    cmdexec(befehl)
    Gtk.main_quit()

  def open_main(self,button):
    global win
    win.show_all()
    self.destroy()
    



ONLINE=False
if cmdexec("xlinux-checkonline").strip()=="ja":
  ONLINE=True

win=HelpWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
